// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import AudioManager, { AudioEffectType } from "../../Managers/AudioManager";
import SaveManager from "../../Managers/SaveManager";
import TipsTxtManager from "../../Managers/TipsTxtManager";
import UIManager from "../../Managers/UIManager";
import TaskManager from "../Tasks/TaskManager";
import SkillEntity from "./SkillEntity";
import SkillTemplate from "./SkillTemplate";

const { ccclass, property } = cc._decorator;

export enum SkillEvent {
    SKILL_UNLOCK_EVENT = "skill_unlock_event",
    SKILL_UPGRADE_EVENT = "skill_upgrade_event"
}

@ccclass
export default class SkillManager implements IModelManager {

    private static _single: SkillManager = null;
    private skillMap: Map<number, SkillTemplate>;
    public static get single(): SkillManager {
        if (SkillManager._single == null) {
            SkillManager._single = new SkillManager();
        }
        return SkillManager._single;
    }

    initConfig(): void {
        this.skillMap = new Map();

        var jsonAss = cc.resources.get("configs/skill", cc.JsonAsset);
        for (const iterator of jsonAss.json) {
            var skilltp = new SkillTemplate();
            skilltp.init(iterator);
            this.skillMap.set(skilltp.id, skilltp);
        }
    }

    getSkillTemplate(id: number): SkillTemplate {
        return this.skillMap.get(id);
    }
    getSkillArr(): SkillTemplate[] {
        var arr: SkillTemplate[] = [];

        this.skillMap.forEach((value: SkillTemplate, key: number, map: Map<number, SkillTemplate>) => {
            arr.push(value);
        });

        return arr;
    }

    getSkillEntity(skillid: number): SkillEntity {
        var userdata = SaveManager.single.userdata;
        return userdata.getSkillEntity(skillid);
    }

    isUnlockSkill(skillid: number): boolean {
        return SaveManager.single.userdata.isUnlockSkill(skillid);
    }


    requestUnlockSkill(skillid: number) {
        var userdata = SaveManager.single.userdata;
        if (userdata.isUnlockSkill(skillid)) {
            return;
        }
        var skillTp = SkillManager.single.getSkillTemplate(skillid);
        var ey = new SkillEntity();
        ey.init({ id: skillid, level: 0 });
        userdata.skillList.push(ey);
        userdata.gold -= skillTp.unlockCost;
        TaskManager.single.requestUpdataTaskPregress(104, skillTp.unlockCost);

        SaveManager.single.saveData();
        UIManager.single.showTips("解锁成功。");
        GameEvent.eventNode.emit(SkillEvent.SKILL_UNLOCK_EVENT);
        GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
    }

    requestUpgradeSkill(skillid: number) {
        var userdata = SaveManager.single.userdata;
        var ey = userdata.getSkillEntity(skillid);
        userdata.gold -= ey.upgradeGoldNum;
        TaskManager.single.requestUpdataTaskPregress(104, ey.upgradeGoldNum);
        ey.level++;
        SaveManager.single.saveData();
        UIManager.single.showTips("升级成功。");
        AudioManager.single.playEffect(AudioEffectType.upgrade);
        GameEvent.eventNode.emit(SkillEvent.SKILL_UPGRADE_EVENT);
        GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
    }




}
